package com.ylt.lock8;

import java.util.concurrent.TimeUnit;

/**
 * 5.增加两个静态的同步方法,静态方法锁的是Phone03.class，先打电话，再发短信
 * 6.两个对象!增加两个静态的同步方法,静态方法锁的是Phone03.class，先打电话，再发短信
 */
public class Test03 {
    public static void main(String[] args) {
        //两个对象
        Phone03 phone01 = new Phone03();
        new Thread(() -> {
            phone01.sendSms();
        }, "A").start();

        //捕获异常
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        new Thread(() -> {
            phone01.call();
        }, "B").start();

    }
}

class Phone03 {

    //锁的对象是方法调用者
    //两个方法用同一个锁，谁先拿到谁先执行
    public static synchronized void sendSms() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }

    public static synchronized void call() {
        System.out.println("打电话");
    }

}